package com.qcby.finance.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.finance.base.entity.dto.SpuDTO;
import com.qcby.finance.base.entity.po.SpuPo;
import com.qcby.finance.base.mapper.SpuMapper;
import com.qcby.finance.base.service.SpuService;
import com.qcby.framework.common.enums.DeleteFlagEnum;
import com.qcby.framework.common.pojo.PageParam;
import com.qcby.framework.common.pojo.PageResult;
import com.qcby.framework.common.pojo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 劝书
 * @since 2023-02-19
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, SpuPo> implements SpuService {

    @Resource
    private SpuMapper spuMapper;

    /**
     * spu展示
     *
     * @param spuDTO    spu dto
     * @param pageParam 页面参数
     * @return {@link Result}<{@link PageResult}<{@link SpuDTO}>>
     */
    @Override
    public Result<PageResult<SpuDTO>> showSpu(SpuDTO spuDTO, PageParam pageParam) {
        /** 构造查询条件 **/
        LambdaQueryWrapper<SpuPo> lqw = buildWrapper(spuDTO);
        Page<SpuPo> skuPoPage = this.spuMapper
                .selectPage(new Page<>(pageParam.getPageNo(),pageParam.getPageSize()),lqw);
        /** po --> dto **/
        List<SpuDTO> skuDTOS = BeanUtil.copyToList(skuPoPage.getRecords(),SpuDTO.class );
        /** 构造返回体 **/
        PageResult pageResult = new PageResult();
        pageResult.setList(skuDTOS);
        pageResult.setTotal(skuPoPage.getTotal());
        return Result.getSuccessResult(pageResult);
    }

    /**
     * 分组展示spu
     *
     * @param spuDTO    spu dto
     * @param pageParam 页面参数
     * @return {@link Result}<{@link PageResult}<{@link SpuDTO}>>
     */
    @Override
    public Result<PageResult<SpuDTO>> showGroupSpu(SpuDTO spuDTO, PageParam pageParam) {
        /** 构造查询条件 **/
        LambdaQueryWrapper<SpuPo> lqw = buildWrapper(spuDTO);
        Page<SpuPo> spuPoPage = this.spuMapper.selectPage(new Page<>(pageParam.getPageNo(),pageParam.getPageSize()),lqw);
        /** po --> dto **/
        List<SpuDTO> spuDTOS = BeanUtil.copyToList(spuPoPage.getRecords(),SpuDTO.class );
        /** 构造返回体 **/
        PageResult pageResult = new PageResult();
        pageResult.setList(spuDTOS);
        pageResult.setTotal(spuPoPage.getTotal());
        return Result.getSuccessResult(pageResult);
    }

    /**
     * 通过id获取spu
     *
     * @param id id
     * @return {@link SpuDTO}
     */
    @Override
    public SpuDTO getSpuById(Long id) {
        LambdaQueryWrapper<SpuPo> lqw = new LambdaQueryWrapper<SpuPo>().eq(SpuPo::getId,id).eq(SpuPo::getDelFlag, DeleteFlagEnum.NO_DELETE.getCode());
        SpuPo spuPo = spuMapper.selectOne(lqw);
        return BeanUtil.copyProperties(spuPo,SpuDTO.class);
    }

    /**
     * 添加spu
     *
     * @param spuDTO spu dto
     * @return int
     */
    @Override
    public int addSpu(SpuDTO spuDTO) {
        SpuPo spuPo = BeanUtil.copyProperties(spuDTO,SpuPo.class);
        return spuMapper.insert(spuPo);
    }

    /**
     * 编辑spu
     *
     * @param spuDTO spu dto
     * @return int
     */
    @Override
    public int editSpu(SpuDTO spuDTO) {
        SpuPo skuPo = BeanUtil.copyProperties(spuDTO,SpuPo.class);
        return spuMapper.updateById(skuPo);

    }

    /**
     * 删除spu
     *
     * @param id id
     * @return {@link Boolean}
     */
    @Override
    public Boolean deleteSpu(Long id) {
        LambdaUpdateWrapper<SpuPo> updateWrapper = new LambdaUpdateWrapper<SpuPo>().eq(SpuPo::getId,id).set(SpuPo::getDelFlag, DeleteFlagEnum.DELETE.getCode());
        int update = spuMapper.update(null, updateWrapper);
        return update > 0;
    }

    /**
     * 构建查询条件
     *
     * @param demoDTO 演示dto
     * @return {@link LambdaQueryWrapper}<{@link SpuPo}>
     */
    public LambdaQueryWrapper<SpuPo> buildWrapper(SpuDTO demoDTO){
        LambdaQueryWrapper<SpuPo> lqw = new LambdaQueryWrapper<SpuPo>();
        /**此处写自己的分页查询参数**/
        lqw.eq(StrUtil.isNotBlank(demoDTO.getSpuNo()),SpuPo::getSpuNo, demoDTO.getSpuNo());
        /**根据id去排序，不要根据创建时间**/
        lqw.orderByDesc(SpuPo::getId);
        return lqw;
    }

    /**
     * 上架下架
     *
     * @param spuDTO spu dto
     * @return {@link Boolean}
     */
    @Override
    public Boolean isOnSale(SpuDTO spuDTO) {
        SpuPo spuPo = BeanUtil.copyProperties(spuDTO,SpuPo.class);
        LambdaUpdateWrapper<SpuPo> updateWrapper = new LambdaUpdateWrapper<SpuPo>()
                .eq(SpuPo::getSpuName,spuPo.getSpuName())
                .set(SpuPo::getOnSale,spuPo.getOnSale());
        return spuMapper.update(null,updateWrapper)>0;
    }

    /**
     * 增加库存数量
     *
     * @param spuNo spu编号
     * @param count 数量
     * @return {@link Boolean}
     */
    @Override
    public Boolean addCount(String spuNo,Integer count) {
        LambdaQueryWrapper<SpuPo> spuPO = new LambdaQueryWrapper<SpuPo>().eq(SpuPo::getSpuNo, spuNo);
        SpuPo spuPO1 = spuMapper.selectOne(spuPO);
        spuPO1.setCount(spuPO1.getCount()+count);
        return true;
    }

    /**
     * 减少库存数量
     *
     * @param spuNo spu编号
     * @param count 数量
     * @return {@link Boolean}
     */
    @Override
    public Boolean reduceCount(String spuNo,Integer count) {
        LambdaQueryWrapper<SpuPo> spuPO = new LambdaQueryWrapper<SpuPo>().eq(SpuPo::getSpuNo, spuNo);
        SpuPo spuPO1 = spuMapper.selectOne(spuPO);
        spuPO1.setCount(spuPO1.getCount()-count);
        return true;
    }

    @Override
    public List<SpuDTO> selectList(List<SpuDTO> list) {
        List<SpuPo> list1 = BeanUtil.copyToList(list, SpuPo.class);
        LambdaQueryWrapper<SpuPo> lqwSpu= new LambdaQueryWrapper<SpuPo>().in(SpuPo::getSpuNo,list1);
        List<SpuPo> spuPos = spuMapper.selectList(lqwSpu);
        List<SpuDTO> spuDTOS = BeanUtil.copyToList(spuPos, SpuDTO.class);
        return spuDTOS;
    }


}
